import 'dart:ffi';
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:syncfusion_flutter_sliders/sliders.dart';
import 'package:video_player/video_player.dart';

class VideoView extends StatefulWidget {
  final double width;
  final double height;
  final String url;
  final SfRangeValues rvalues;
  Function(Map m) aspect;
  VideoView({
    super.key,
    required this.width,
    required this.height,
    required this.url,
    required this.aspect,
    required this.rvalues,
  });

  @override
  State<VideoView> createState() => _VideoViewState();
}

class _VideoViewState extends State<VideoView> {
  late VideoPlayerController _controller;
  late double _w;
  late double _h;
  @override
  void initState() {
    super.initState();
    print("------->url---${widget.url}");
    print("------->value1---${widget.rvalues}");
    if (widget.url != null) {
      _controller = VideoPlayerController.file(File(widget.url))
        ..initialize().then((_) {
          setState(() {
            Map map = {};
            map["asp"] = _controller.value.aspectRatio;
            map["size"] = _controller.value.copyWith().size;
            map["duration"] =
                (_controller.value.duration.inMilliseconds / 1000).truncate();
            map['con'] = _controller;
            widget.aspect(map);
            _h = _w / _controller.value.aspectRatio;
          });
          _controller.setLooping(true);
          _controller.play();
        });
    }
  }

  @override
  Widget build(BuildContext context) {
    _w = widget.width;
    _h = widget.height;
    return Container(
      width: _w,
      height: _h,
      child: _controller.value.isInitialized
          ? VideoPlayer(_controller)
          : Container(
              color: Colors.black,
            ),
      // child : VideoPlayer(_controller),
    );
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }
}
